package com.zmn.oms.dubbo.impl.zmn.serviceprovider.order;

import com.zmn.oms.common.enums.EncryptFieldTypeEnum;
import org.apache.dubbo.config.annotation.Service;
import com.google.common.collect.Lists;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto.ResponseDTO;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.common.utils.response.ResponseUtil;
import com.zmn.common.utils.validator.ValidateResult;
import com.zmn.common.utils.validator.ValidatorUtil;
import com.zmn.oms.business.interfaces.work.OrderWorkListBService;
import com.zmn.oms.common.constant.OrderStatusConsts;
import com.zmn.oms.dubbo.dto.zmn.order.serviceprovider.OrderReceiveDIO;
import com.zmn.oms.dubbo.dto.zmn.order.serviceprovider.ReceiveDIO;
import com.zmn.oms.dubbo.dto.zmn.order.serviceprovider.ReceiveDRO;
import com.zmn.oms.dubbo.dto.zmn.order.serviceprovider.ReceivePageDRO;
import com.zmn.oms.dubbo.impl.zmn.order.OrderBaseRemoteService;
import com.zmn.oms.dubbo.interfaces.zmn.serviceprovider.order.OrderReceiveRemoteService;
import com.zmn.oms.zmn.business.interfaces.work.OrderWorkAssignConfirmBService;
import com.zmn.oms.dubbo.utils.DubboConsts;
import com.zmn.oms.model.dto.work.modify.must.OrderAssignConfirmDTO;
import com.zmn.oms.model.entity.work.OrderWorkQuery;
import com.zmn.base.oms.common.model.es.orderwork.EsOrderWork;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Objects;

/**
 * 类描述：服务商  订单操作-接单
 *
 * @author yule
 * @date 2019/05/16 16:05
 */
@Deprecated
@Service(loadbalance = "roundrobin", timeout = 5000, retries = 2, version = DubboConsts.INTERFACE_VERSION, validation = "false")
public class OrderReceiveRemoteServiceImpl extends OrderBaseRemoteService implements OrderReceiveRemoteService {

	private static final String TAG = "服务商-接单";
	private Logger logger = LoggerFactory.getLogger(OrderReceiveRemoteServiceImpl.class);

	@Autowired
	private OrderWorkListBService orderWorkListBService;
	@Autowired
	private OrderWorkAssignConfirmBService orderWorkAssignConfirmBService;


	@Override
	public ResponseDTO<ReceivePageDRO> receiveOrderList(ReceiveDIO receiveDIO) {
		Objects.requireNonNull(receiveDIO.getCompanyId());

		OrderWorkQuery query = new OrderWorkQuery();
		query.setWorkStatus(Lists.newArrayList(OrderStatusConsts.WORK_STATUS_ASSIGN));//已分单
		query.setWorkResultStatus(OrderStatusConsts.WORK_RESULT_DOING);//进行中
		query.setExistAssignConfirmTime(GlobalConsts.NO);
		query.setPageIndex(receiveDIO.getPageIndex());
		query.setPageSize(receiveDIO.getPageSize());
		query.setDefaultOrder(GlobalConsts.YES);
		query.setOrderField("assignTime");
		//query.setCompanyIdList(Lists.newArrayList(receiveDIO.getCompanyId()));
		query.setManageCompanyIdList(Lists.newArrayList(receiveDIO.getCompanyId()));//服务商Id
		query.setPlatWork(receiveDIO.getPlat());//平台标识

		//query.setExcludes(Arrays.asList("categOneName,", "categTwoName", "productInfo", "orderId,", "servCategName", "assignTime", "contact", "provinceName", "cityName", "countyName", "orderId", "productList", "factoryOrder"));
		// 查询
		List<EsOrderWork> esOrderWorks = orderWorkListBService.listPageOrderWorkByQuery(query);
		// 转 vo
		List<ReceiveDRO> list = Lists.newArrayList();
		ReceivePageDRO pageDRO = new ReceivePageDRO();

		if (esOrderWorks == null) {
			return ResponseUtil.responseSuccess(pageDRO, GlobalConsts.SUCCESS_MSG);
		}
		// 解密电话和地址
		orderEncryptService.batchDecryptEsOrderWork(esOrderWorks, EncryptFieldTypeEnum.USER_TELPHONE, EncryptFieldTypeEnum.USER_ADDRESS);

		esOrderWorks.forEach((esWork -> {
			ReceiveDRO vo = BeanMapper.map(esWork, ReceiveDRO.class);
			vo.setAddress(esWork.getContact() == null ? null : esWork.getContact().getAddress());
			vo.setStreet(esWork.getContact() == null ? null : esWork.getContact().getStreet());
			vo.setPhone(esWork.getContact() == null ? null : esWork.getContact().getPhone());
			vo.setUserName(esWork.getContact() == null ? null : esWork.getContact().getUserName());
			vo.setPhone(esWork.getContact() == null ? null : esWork.getContact().getPhone());
			vo.setProvinceName(esWork.getProvinceName());
			vo.setCityName(esWork.getCityName());
			vo.setCountyName(esWork.getCountyName());
			vo.setWorkId(esWork.getId());
			vo.setProductCount(esWork.getProductList() == null ? null : esWork.getProductList().get(0).getNumber());
			if (esWork.getFactoryOrder() == null) {
				vo.setProductName((esWork.getProductList() == null ? null : esWork.getProductList().get(0).getProductName()));
			} else {
				vo.setProductName(esWork.getFactoryOrder().getProductName());
			}
			vo.setWorkId(esWork.getId());
			list.add(vo);
		}));

		pageDRO.setTotalCount(query.getTotalCount());
		pageDRO.setList(list);

		return ResponseUtil.responseSuccess(pageDRO, GlobalConsts.SUCCESS_MSG);
	}

	@Override
	public ResponseDTO receiveOrder(OrderReceiveDIO receiveOrderDIO) {
		logger.info("[{}] 确认接单：{}", TAG, receiveOrderDIO);

		ValidateResult validateResult = ValidatorUtil.validator(receiveOrderDIO);

		if (!validateResult.isSuccess()) {
			logger.error("[{}] 参数错误：{}", TAG, validateResult.toString());
			return ResponseUtil.responseFail("提交参数错误：" + validateResult.toString());
		}

		//已分单 -> 已分单
		try {
			OrderAssignConfirmDTO orderAssignConfirmDTO = new OrderAssignConfirmDTO();
			orderAssignConfirmDTO.setOrderId(receiveOrderDIO.getOrderId());
			orderAssignConfirmDTO.setWorkId(receiveOrderDIO.getWorkId());
			orderAssignConfirmDTO.setOperatorId(receiveOrderDIO.getOperatorId());
			orderAssignConfirmDTO.setOperator(GlobalConsts.getOperatorTypeName(GlobalConsts.OPERATE_USER_TYPE_SERVICEPROVIDER) + "-" + receiveOrderDIO.getOperator());
			orderAssignConfirmDTO.setOperatorType(GlobalConsts.OPERATE_USER_TYPE_SERVICEPROVIDER);
			orderWorkAssignConfirmBService.saveAssignConfirm(orderAssignConfirmDTO);
			return ResponseUtil.responseSuccess();
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseUtil.responseFail(e.getMessage());
		}
	}

}
